package com.guanyong.webapi.controllers.api;

import com.guanyong.model.VideoSourceModel;
import com.guanyong.services.VideoSourceService;
import com.guanyong.utils.Comm.ResultCode;
import com.guanyong.utils.Comm.ResultObject;
import com.guanyong.webapi.config.CreateVideoURLUtil;
import com.guanyong.webapi.config.VideoLocalUtil;
import com.guanyong.webapi.config.VoucherUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by mxk on 2017/7/16.
 */
@RestController
@RequestMapping("/api/videoloal/")
public class VideoLocal {

    @Autowired
    VideoSourceService videoSourceService;



    @Autowired
    CreateVideoURLUtil createVideoURLUtil;

    @RequestMapping(value = "getvideoLocal", method = RequestMethod.POST)
    public ResultObject<Map<String,Object>> Getvideovoucher(HttpServletRequest request,
                                                            @RequestParam String objectid,
                                                            @RequestParam Integer classtype){

        ResultObject<Map<String,Object>> resultObject=new
                ResultObject<Map<String,Object>>();
        VideoSourceModel videoSourceModel=videoSourceService.
                GetVideoScorceByObjectIdType(classtype,
                        objectid,2);
        if (videoSourceModel!=null){
            Map<String,Object> map = new HashMap<String, Object>();
            String playAuth = null;

//            playAuth=videoSourceModel.getfSourceLocal();
            playAuth=createVideoURLUtil.getVideoURL(videoSourceModel.getfSourceLocal());
            if (playAuth!=null){
                map.put("objectId",objectid);
                map.put("videoId",videoSourceModel.getfSourceurl());
                map.put("videoVoucher",playAuth);
                resultObject.setResult(map);
            }else {
                resultObject.setCode(ResultCode.NoResult);
            }
        }else{
            resultObject.setCode(ResultCode.NoResult);
        }
        return  resultObject;

    }


    @RequestMapping(value = "getvideoLocal1_0", method = RequestMethod.POST)
    public ResultObject<Map<String,Object>> Getvideovoucher1_0(HttpServletRequest request,
                                                            @RequestParam String objectid,
                                                            @RequestParam Integer classtype,
                                                               @RequestParam Integer fSourcetype  ){

        ResultObject<Map<String,Object>> resultObject=new
                ResultObject<Map<String,Object>>();
        VideoSourceModel videoSourceModel=videoSourceService.
                GetVideoScorceByObjectIdType(classtype,
                        objectid,fSourcetype);
        if (videoSourceModel!=null){


            Map<String,Object> map = new HashMap<String, Object>();
            String playAuth = null;
            if (videoSourceModel.getfSourceurl()!=null){
                playAuth=createVideoURLUtil.getVideoURL(videoSourceModel.getfSourceLocal());
            }
            if (playAuth!=null){
                map.put("objectId",objectid);
                map.put("videoId",videoSourceModel.getfSourceurl());
                map.put("videoVoucher",playAuth);
                resultObject.setResult(map);
            }else {
                resultObject.setCode(ResultCode.NoResult);
            }
        }else{
            resultObject.setCode(ResultCode.NoResult);
        }
        return  resultObject;

    }


}
